package com.loongtech.bi.action.birealtime;

import com.loongtech.bi.action.TransferHelper;
import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntityBiPlatform;
import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.manager.realTime.OverviewManager;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewData;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.bi.manager.realTime.ServerRealTimeManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 
 * ClassName: OverviewBiAction
 * 
 * @Description: 实时数据 —— 实时概况
 * @author liuchen
 * @date 2017-5-24 上午10:33:00
 */
@Controller
@Scope("prototype")
@RequestMapping("/realTime/overview")
public class OverviewBiAction extends TransferHelper {
	private static final long serialVersionUID = 1L;
	private Map<String, String> platformItmes; // 平台下拉列表
	private Map<String, String> channelItmes; // 渠道下拉列表
	private Map<String, String> serverItmes; // 区服下拉列表

	private List<ServerRealTimeManager.OnlineRealTimeData> onlineRealTimeDatas; // 项目整体实时在线数据
	private ChartUIData onlineRealTimeChart; // 项目整体实时在线图表

	private List<OverviewData> datas; // 前端要显示的实时汇总数据
	private List<OverviewDetailData> detailDatas; // 前端要显示的详细数据

	private ChartUIData lineChart2; // 前端要显示的曲线图数据-新增帐号数
	private ChartUIData lineChart20; // 前端要显示的曲线图数据-去重新增帐号数
	private ChartUIData lineChart3; // 前端要显示的曲线图数据-活跃帐号数
	private ChartUIData lineChart4; // 前端要显示的曲线图数据-付费帐号数
	private ChartUIData lineChart5; // 前端要显示的曲线图数据-付费金额
	private ChartUIData lineChart6; // 前端要显示的曲线图数据-新增帐号付费数
	private ChartUIData lineChart7; // 前端要显示的曲线图数据-新增帐号付费金额

	private List<OverviewDetailData> detailDatas1; // 前端要显示的详细数据
	private ChartUIData lineChart21; // 前端要显示的曲线图数据-新增帐号数
	private ChartUIData lineChart210; // 前端要显示的曲线图数据-去重新增帐号数
	private ChartUIData lineChart31; // 前端要显示的曲线图数据-活跃帐号数
	private ChartUIData lineChart41; // 前端要显示的曲线图数据-付费帐号数
	private ChartUIData lineChart51; // 前端要显示的曲线图数据-付费金额
	private ChartUIData lineChart61; // 前端要显示的曲线图数据-新增帐号付费数
	private ChartUIData lineChart71; // 前端要显示的曲线图数据-新增帐号付费金额
	private ChartUIData lineChart81; // 前端要显示的曲线图数据-分时累计活跃账号

	@Autowired
	OverviewManager overviewManager;
	@Autowired
	private ServerRealTimeManager serverRealTimeManager;

	@RequestMapping(value = "initParam.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
		try {
			Map<String, Object> result = new HashMap<String, Object>();
			// 平台
			platformItmes = new LinkedHashMap<>();
			platformItmes.put("全平台", "0");
			for (EntityBiPlatform p : BiPlatformManager.PLATFORM_LIST) {
				platformItmes.put(p.getName(), p.getId());
			}
			// 区服
			serverItmes = new LinkedHashMap<>();
			serverItmes.put("全区服", "0");
			for (EntityBiServer s : BiPlatformManager.SERVER_LIST) {
				serverItmes.put(s.getName(), s.getId());
			}
			// 渠道
			channelItmes = new LinkedHashMap<>();
			channelItmes.put("全渠道", "0");
			for (EntityBiChannel c : BiPlatformManager.getChannelIdsByPlatformId("0")) {
				channelItmes.put(c.getName(), c.getId());
			}
			result.put("res", "0");
			result.put("serverItmes", serverItmes);
			result.put("channelItmes", channelItmes);
			result.put("platformItmes", platformItmes);
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
			return;
		}
	}

	@RequestMapping(value = "search.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String date,
			@RequestParam String cdate, @RequestParam String platformid, @RequestParam String serverid,
			@RequestParam String channelid, String callback) {

        Map<String, Object> result = new HashMap<String, Object>();
		try {
			Date lookDate = Time.formatDate(date, "yyyy-MM-dd");
			Date compareDate = Time.formatDate(cdate, "yyyy-MM-dd");
			Date lastWeekDate = new Date(Time.getBeginningOfDay(lookDate.getTime() - 7 * Time.DAY)); // 查看时间前几天的时间
			boolean ignore = Time.getInterval(Time.currentDate(), lookDate) != 0
					|| Time.getInterval(new Date(Time.currentTime() - Time.DAY), compareDate) != 0;

			lineChart2 = null;
			lineChart20 = null;
			lineChart3 = null;
			lineChart4 = null;
			lineChart5 = null;
			lineChart6 = null;
			lineChart7 = null;

			lineChart21 = null;
			lineChart210 = null;
			lineChart31 = null;
			lineChart41 = null;
			lineChart51 = null;
			lineChart61 = null;
			lineChart71 = null;
			lineChart81 = null;
			datas = overviewManager.getOverviewData(lookDate, platformid, channelid, serverid);
			detailDatas = overviewManager.getOverviewDetailData("1", lookDate, compareDate, lastWeekDate, platformid, channelid, serverid); // 分时详细数据

			if (detailDatas != null && detailDatas.size() > 0) {
				lineChart2 = new ChartUIData();
				lineChart2.setSelectItem("newUserLookValue");
				lineChart20 = new ChartUIData();
				lineChart20.setSelectItem("distinctNewUserLookValue");
				lineChart3 = new ChartUIData();
				lineChart3.setSelectItem("activeUserLookValue");
				lineChart4 = new ChartUIData();
				lineChart4.setSelectItem("payUserLookValue");
				lineChart5 = new ChartUIData();
				lineChart5.setSelectItem("payLookValue");
				lineChart6 = new ChartUIData();
				lineChart6.setSelectItem("newPayUserLookValue");
				lineChart7 = new ChartUIData();
				lineChart7.setSelectItem("newUserPayLookValue");
				if (ignore) {
					lineChart2.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart2
							.getSelectItem(), "newUserLastWeekValue").loadData(lineChart2.getJsonType()));
					lineChart20.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart20
							.getSelectItem(), "distinctNewUserLastWeekValue").loadData(lineChart20.getJsonType()));
					lineChart3.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart3
							.getSelectItem(), "activeUserLastWeekValue").loadData(lineChart3.getJsonType()));
					lineChart4.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart4
							.getSelectItem(), "payUserLastWeekValue").loadData(lineChart4.getJsonType()));
					lineChart5.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart5
							.getSelectItem(), "payLastWeekValue").loadData(lineChart5.getJsonType()));
					lineChart6.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart6
							.getSelectItem(), "newPayUserLastWeekValue").loadData(lineChart6.getJsonType()));
					lineChart7.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart7
							.getSelectItem(), "newUserPayLastWeekValue").loadData(lineChart7.getJsonType()));
				} else {
					lineChart2.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart2
							.getSelectItem()).loadData(lineChart2.getJsonType()));
					lineChart20.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart20
							.getSelectItem()).loadData(lineChart20.getJsonType()));
					lineChart3.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart3
							.getSelectItem()).loadData(lineChart3.getJsonType()));
					lineChart4.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart4
							.getSelectItem()).loadData(lineChart4.getJsonType()));
					lineChart5.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart5
							.getSelectItem()).loadData(lineChart5.getJsonType()));
					lineChart6.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart6
							.getSelectItem()).loadData(lineChart6.getJsonType()));
					lineChart7.setJsonStr(new ChartTool(detailDatas, OverviewDetailData.class, lineChart7
							.getSelectItem()).loadData(lineChart7.getJsonType()));
				}

				Collections.sort(detailDatas, overviewManager.new OverviewDetailData().comparatorHour2);
			}

			detailDatas1 = overviewManager.getOverviewDetailData("2", lookDate, compareDate, lastWeekDate, platformid, channelid, serverid); // 累计详细数据

			if (detailDatas1 != null && detailDatas1.size() > 0) {
				lineChart21 = new ChartUIData();
				lineChart21.setSelectItem("newUserLookValue");
				lineChart210 = new ChartUIData();
				lineChart210.setSelectItem("distinctNewUserLookValue");
				lineChart31 = new ChartUIData();
				lineChart31.setSelectItem("activeUserLookValue");
				lineChart41 = new ChartUIData();
				lineChart41.setSelectItem("payUserLookValue");
				lineChart51 = new ChartUIData();
				lineChart51.setSelectItem("payLookValue");
				lineChart61 = new ChartUIData();
				lineChart61.setSelectItem("newPayUserLookValue");
				lineChart71 = new ChartUIData();
				lineChart71.setSelectItem("newUserPayLookValue");
				lineChart81 = new ChartUIData();
				lineChart81.setSelectItem("totalActiveUserLookValue");
				if (ignore) {
					lineChart21.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart21
							.getSelectItem(), "newUserLastWeekValue").loadData(lineChart21.getJsonType()));
					lineChart210.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart210
							.getSelectItem(), "distinctNewUserLastWeekValue").loadData(lineChart210.getJsonType()));
					lineChart31.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart31
							.getSelectItem(), "activeUserLastWeekValue").loadData(lineChart31.getJsonType()));
					lineChart41.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart41
							.getSelectItem(), "payUserLastWeekValue").loadData(lineChart41.getJsonType()));
					lineChart51.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart51
							.getSelectItem(), "payLastWeekValue").loadData(lineChart51.getJsonType()));
					lineChart61.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart61
							.getSelectItem(), "newPayUserLastWeekValue").loadData(lineChart61.getJsonType()));
					lineChart71.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart71
							.getSelectItem(), "newUserPayLastWeekValue").loadData(lineChart71.getJsonType()));
					lineChart81.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart81
							.getSelectItem(), "totalActiveUserLastWeekValue").loadData(lineChart81.getJsonType()));
				} else {
					lineChart21.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart21
							.getSelectItem()).loadData(lineChart21.getJsonType()));
					lineChart210.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart210
							.getSelectItem()).loadData(lineChart210.getJsonType()));
					lineChart31.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart31
							.getSelectItem()).loadData(lineChart31.getJsonType()));
					lineChart41.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart41
							.getSelectItem()).loadData(lineChart41.getJsonType()));
					lineChart51.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart51
							.getSelectItem()).loadData(lineChart51.getJsonType()));
					lineChart61.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart61
							.getSelectItem()).loadData(lineChart61.getJsonType()));
					lineChart71.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart71
							.getSelectItem()).loadData(lineChart71.getJsonType()));
					lineChart81.setJsonStr(new ChartTool(detailDatas1, OverviewDetailData.class, lineChart81
							.getSelectItem()).loadData(lineChart81.getJsonType()));
				}

				Collections.sort(detailDatas1, overviewManager.new OverviewDetailData().comparatorHour2);
			}
			onlineRealTimeDatas = new ArrayList<>();
			onlineRealTimeChart = null; // 项目整体实时在线图表

			onlineRealTimeDatas = serverRealTimeManager.getOnlineRealTimeDatas(lookDate, compareDate, lastWeekDate, platformid, channelid, serverid);  // 整体实时在线

			if (onlineRealTimeDatas != null && onlineRealTimeDatas.size() > 0) {
				onlineRealTimeChart = new ChartUIData();
				onlineRealTimeChart.setJsonStr(new ChartTool(onlineRealTimeDatas,
						ServerRealTimeManager.OnlineRealTimeData.class, onlineRealTimeChart.getSelectItem())
						.loadData(onlineRealTimeChart.getJsonType()));
			} else {
				onlineRealTimeChart = null;
			}
			result.put("res", "0");
			result.put("datas", datas);
			result.put("onlineRealTimeChart", onlineRealTimeChart); // 整体实时在线
			result.put("lineChart21", lineChart21); // 分小时累计数据-新增账号数
			// result.put("lineChart210", lineChart210);// 分小时累计数据-去重新增账号数
			// result.put("lineChart31", lineChart31);// 分小时累计数据-活跃账号数
			// result.put("lineChart41", lineChart41);// 分小时累计数据-付费账号数
			result.put("lineChart51", lineChart51);// 分小时累计数据-付费金额
			// result.put("lineChart61", lineChart61);// 分小时累计数据-新增账号付费人数
			// result.put("lineChart71", lineChart71);// 分小时累计数据-增账号付费金额
			result.put("lineChart81", lineChart81);   // 分小时累计活跃账号数
			result.put("detailDatas1", detailDatas1);// 分小时累计详细数据
			result.put("lineChart2", lineChart2);// 分小时数据-新增账号数
			//result.put("lineChart20", lineChart20);// 分小时数据-去重新增账号数
			//result.put("lineChart3", lineChart3);// 分小时数据-活跃账号数
			result.put("lineChart4", lineChart4);// 分小时数据-付费账号数
			result.put("lineChart5", lineChart5);// 分小时数据-付费金额
			result.put("lineChart6", lineChart6);// 分小时数据-新增账号付费人数
			result.put("lineChart7", lineChart7);// 分小时数据-新增账号付费金额
			result.put("detailDatas", detailDatas);// 分小时详细数据
			printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
		}
	}
}